//页面初始化
import {resetTabData} from "../../../other/js/manageTab/tab_fun";

export const init = (self, tiyData) => {
  self.$ySpliceAll(tiyData.tData.tabList, tiyData.tData.mappingList, tiyData.tData.categoryList, tiyData.tParam.selectedMappings);
  resetTabData(tiyData.tData.tab);
  tiyData.tParam.tab.id = "";
  tiyData.tParam.tab.name = "";
  tiyData.tParam.tab.type = 0;
  tiyData.tParam.isAddMappings = true;
  tiyData.tParam.tabId = "";
  tiyData.tStatus.mappingModal.isShow = false;
}

//查询标签列表
export const listTabs = (self, tiyData) => {
  tiyData.tPage.tabPage.tableLoading = true;
  tiyData.tPage.tabPage.noDataText = "正在查询...";
  self.$ySpliceAll(tiyData.tData.tabList);
  //参数
  let param = {
    "name": tiyData.tParam.tab.name,
    // "type": tiyData.tParam.tab.type, //暂时只显示产品管理标签
    "type": 1,
    "pageNumber": tiyData.tPage.tabPage.currentPage,
    "pageSize": tiyData.tPage.tabPage.stepNumber,
  };
  //查询tabs
  self.$apiOther.pageQueryManageTabs(param).then(response => {
    setTabsTable(self, response, tiyData);
  }).catch(() => {
    tiyData.tPage.tabPage.tableLoading = false;
    tiyData.tPage.tabPage.noDataText = "查询失败";
  });
}
//处理管理标签查询结果
export const setTabsTable = (self, response, tiyData) => {
  if(response.success) {
    tiyData.tData.tabList.splice(0,tiyData.tData.tabList.length);
    let page = response.result;
    if(page.list!=null && page.list.length > 0) {
      page.list.forEach(function(item){
        tiyData.tData.tabList.push(item);
      });
    }else{
      tiyData.tPage.tabPage.noDataText = "暂无数据";
    }
    //分页
    tiyData.tPage.tabPage.total = page.total;
    tiyData.tPage.tabPage.currentPage = page.pageNum==0?1:page.pageNum;
    tiyData.tPage.tabPage.stepNumber = page.pageSize;
  }else{
    self.$Message.error(response.errorMessage);
  }
  tiyData.tPage.tabPage.tableLoading = false;
}

//标签类型名称
export const getTypeName = (type) => {
  switch (type) {
    case 1:
      return "产品";
    case 2:
      return "用户";
    case 3:
      return "订单";
    default:
      return "";
  }
}

//查询产品列表
export const queryProducts = (self, tiyData) => {
  tiyData.tPage.mappingPage.tableLoading = true;
  self.$ySpliceAll(tiyData.tData.mappingList);
  let params = getCommonParams(tiyData);
  params.categoryId = tiyData.tParam.chosenCategory.id;
  params.categoryLevel = tiyData.tParam.chosenCategory.level;
  //查询产品列表
  self.$apiProduct.findManageTabProducts(params).then(response => {
    setMappingsTable(self, response, tiyData);
  });
}

//通用参数
export const getCommonParams = (tiyData) => {
  return {
    tabId: tiyData.tParam.tabId,
    isAdd: tiyData.tParam.isAddMappings,
    pageNumber: tiyData.tPage.mappingPage.currentPage,
    pageSize: tiyData.tPage.mappingPage.stepNumber,
    search: tiyData.tParam.search,
  };
}

//处理管理标签映射查询结果
export const setMappingsTable = (self, response, tiyData) => {
  if(response.success) {
    let page = response.result;
    if(page.list!=null && page.list.length > 0) {
      page.list.forEach(function(item){
        tiyData.tData.mappingList.push(item);
      });
    }else{
      tiyData.tPage.mappingPage.noDataText = "暂无数据";
    }
    //分页
    tiyData.tPage.mappingPage.total = page.total;
    tiyData.tPage.mappingPage.currentPage = page.pageNum==0?1:page.pageNum;
    tiyData.tPage.mappingPage.stepNumber = page.pageSize;
  }else{
    self.$Message.error(response.errorMessage);
  }
  tiyData.tPage.mappingPage.tableLoading = false;
}

//查询用户列表
export const queryUsers = (self, tiyData) => {

}

//查询订单列表
export const queryOrders = (self, tiyData) => {

}

//保存管理标签映射
export const saveTabMappings = (self, tiyData) => {
  self.$Spin.show();
  let mappingIds = tiyData.tParam.selectedMappings.map(obj => {return obj.id});
  let params = {
    tabId: tiyData.tParam.tabId,
    mappingIds: mappingIds,
  };
  self.$apiOther.saveTabMappings(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("保存成功");
      tiyData.tPage.mappingPage.currentPage = 1;
      this.param.selectedMappings.splice(0, this.param.selectedMappings.length);
      self.queryMappings();
    }else{
      self.$Modal.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Message.error("保存失败，请重试！")
    self.$Spin.hide();
  });
}

//删除管理标签映射
export const deleteTabMappings = (self, tiyData) => {
  self.$Spin.show();
  let mappingIds = tiyData.tParam.selectedMappings.map(obj => {return obj.id});
  let params = {
    tabId: tiyData.tParam.tabId,
    mappingIds: mappingIds,
  };
  self.$apiOther.deleteTabMappings(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("移除成功");
      tiyData.tPage.mappingPage.currentPage = 1;
      this.param.selectedMappings.splice(0, this.param.selectedMappings.length);
      self.queryMappings();
    }else{
      self.$Modal.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Message.error("保存失败，请重试！")
    self.$Spin.hide();
  });
}
